<!-- eslint-disable vue/valid-attribute-name -->
<template>
  <div>
    <el-table
      :data="PermisstionArr"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
    >
      <el-table-column prop="name" label="名称" sortable />
      <el-table-column prop="code" label="权限值" sortable />
      <el-table-column prop="updateTime" label="修改时间" sortable />
      <el-table-column label="操作" sortable>
        <template #="{ row }">
          <el-button
            @click="addPermisstion(row)"
            type="primary"
            size="small"
            :disabled="row.level == 4 ? true : false"
          >
            {{ row.level == 3 ? '添加功能' : '添加菜单' }}
          </el-button>
          <el-button
            @click="updatePermisstion(row)"
            type="primary"
            size="small"
            :disabled="row.level == 1 ? true : false"
          >
            编辑
          </el-button>
          <el-button
            @click="removeMenu(row.id)"
            type="danger"
            size="small"
            :disabled="row.level == 1 ? true : false"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 对话框组件:添加|更新已有菜单的结构 -->
    <el-dialog
      v-model="centerDialogVisible"
      :title="menuData.id ? '更新菜单' : '添加菜单'"
      width="30%"
      align-center
    >
      <el-form label-width="60px">
        <el-form-item label="名称">
          <el-input
            placeholder="请输入菜单名称"
            v-model="menuData.name"
          ></el-input>
        </el-form-item>
        <el-form-item label="权限值">
          <el-input
            placeholder="请输入权限值"
            v-model="menuData.code"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="centerDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="save">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue'
// 引入请求API
import {
  reqAllPermisstion,
  reqAddOrUpdatePermisstion,
  reqRemoveMenu,
} from '../../../api/acl/menu/'
// 引入ts类型
import type {
  PermisstionResponseData,
  PermisstionList,
  Permisstion,
  MenuParams,
} from '../../../api/acl/menu/type'
import { ElMessage } from 'element-plus'
// 存储菜单的数据
let PermisstionArr = ref<PermisstionList>([])
// 控制对话框的显示与隐藏
let centerDialogVisible = ref<boolean>(false)
// 添加|更新菜单携带的参数
let menuData = reactive<MenuParams>({
  code: '',
  level: 0,
  name: '',
  pid: 0,
})

// 组件挂载完毕
onMounted(() => {
  getHasPermisstion()
})
// 封装获取菜单数据的方法
const getHasPermisstion = async () => {
  // 发生请求获取数据
  const result: PermisstionResponseData = await reqAllPermisstion()
  if (result.code == 200) {
    // 存储菜单的数据
    PermisstionArr.value = result.data
  }
}
// 添加菜单的回调
const addPermisstion = (row: Permisstion) => {
  // 清空记录
  Object.assign(menuData, {
    id: 0,
    code: '',
    level: 0,
    name: '',
    pid: 0,
  })
  // 显示对话框
  centerDialogVisible.value = true
  //收集新增的菜单的level数值
  menuData.level = row.level + 1
  // 收集给那个菜单新增子菜单
  menuData.pid = row.id as number
}
// 更新菜单的回调
const updatePermisstion = (row: Permisstion) => {
  // 显示对话框
  centerDialogVisible.value = true
  // 收集更新菜单的数据
  Object.assign(menuData, row)
}
// 对话框组件确定按钮的回调
const save = async () => {
  // 发送添加|更新菜单的请求
  const result: any = await reqAddOrUpdatePermisstion(menuData)
  if (result.code == 200) {
    // 关闭对话框
    centerDialogVisible.value = false
    // 提示信息
    ElMessage({
      type: 'success',
      message: menuData.id ? '更新菜单成功' : '添加菜单成功',
    })
    // 重新获取菜单信息
    getHasPermisstion()
  } else {
    ElMessage({
      type: 'error',
      message: menuData.id ? '系统菜单不能更新' : '添加菜单失败',
    })
  }
}
// 删除菜单的回调
const removeMenu = async (id: number) => {
  // 发送请求删除已有菜单
  const result: any = await reqRemoveMenu(id)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除菜单成功',
    })
    // 重新获取菜单信息
    getHasPermisstion()
  } else {
    ElMessage({
      type: 'error',
      message: '系统菜单不能删除',
    })
  }
}
</script>

<style scoped></style>
